package common.kit;

import java.util.LinkedHashMap;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbTemplate;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class PageSql {
	Kv conditionParams = new Kv(); //用户端传过来的键值对
    String select ;
    String from ;
    LinkedHashMap<String, String> joinList = new LinkedHashMap<String, String>() ;
    LinkedHashMap<String, String> cond =  new LinkedHashMap<String, String>() ;
    String groupBy ;
    String orderBy ;

    
    public PageSql() {
    	
    }
    
    public PageSql(Kv conditionParams) {
    	this.conditionParams = conditionParams ;
    }

    public static PageSql bySelect( Kv conditionParams , String columns){
        return new PageSql(conditionParams).select(columns);
    }
    
    public static PageSql bySelect( String columns){
        return new PageSql().select(columns);
    }

    public PageSql select(String select) {
        this.select = select;
        return this ;
    }

    public PageSql from(String from) {
        this.from = from ;
        return this ;
    }

    public PageSql join(String joinTableName , String on) {
        LinkedHashMap<String, String> joinList = this.joinList ;
        joinList.put(joinTableName , on) ;
        return this ;
    }
    
    public LinkedHashMap<String, String> setEqualCondition(String key) {
		return setEqualCondition(key , null );
	}
    
    public LinkedHashMap<String, String> setEqualCondition(String key , String emptyValue) {
		String value = this.conditionParams.getStr(key);
		if ( StrKit.isBlank(value) || (StrKit.notBlank(emptyValue) && emptyValue.equals(value) ) ) {
			return this.cond ;
		}
		this.cond.put(key + " = ", value);
		return this.cond ;
	}
    
    public LinkedHashMap<String, String> setLikeCondition(String key) {
        String value = this.conditionParams.getStr(key) ;
        if (StrKit.isBlank(value)) {
            return this.cond ;
        }
        this.cond.put(key + " LIKE ", "%" + value + "%");
        return this.cond ;
    }
    
    public LinkedHashMap<String, String> setNotLessThanCondition(  String key , String paramKey) {
		String value = this.conditionParams.getStr(paramKey) ;
		if (StrKit.isBlank(value)) {
			return this.cond ;
		}
		this.cond.put(key + " >= ", value);
		return this.cond;
	}
    
    public LinkedHashMap<String, String> setNotMoreThanCondition( String key , String paramKey) {
		String value = this.conditionParams.getStr(paramKey) ;
		if (StrKit.isBlank(value)) {
			return this.cond ;
		}
		this.cond.put(key + " <= ", value);
		return this.cond ;
	}
    
    public LinkedHashMap<String, String> setInCondition( String key ) {
		String value = this.conditionParams.getStr(key) ;
		if (StrKit.isBlank(value)) {
			return this.cond ;
		}
		this.cond.put(key + " IN (" + value +" ) " , "" );
		return this.cond ;
	}
    
    public PageSql groupBy(String groupBy) {
        this.groupBy = groupBy;
        return this ;
    }

    public PageSql orderBy(String orderBy) {
        this.orderBy = orderBy;
        return this ;
    }
    
    public Kv collectAll() {
    	 Kv kv = Kv.create();
         kv.set("select" , this.select );
         kv.set("from" , this.from );
         kv.set("joinList" , this.joinList ) ;
         kv.set("cond" , this.cond ) ;
         kv.set("groupBy" , this.groupBy );
         kv.set("orderBy" , this.orderBy );
         return kv ;
	}

    public Page<Record> paginate(Integer pageNumber , Integer pageSize){
        Kv kv = collectAll();
        DbTemplate template = Db.template("paginate" , kv);
        return template.paginate(pageNumber , pageSize , StrKit.notBlank(this.groupBy));
    }
    
    public Record findFirst(){
    	Kv kv = collectAll();
        DbTemplate template = Db.template("paginate" , kv);
        return template.findFirst();
    }
    
    public List<Record> find(){
   	 	Kv kv = collectAll();
	    DbTemplate template = Db.template("paginate" , kv);
	    return template.find();
    }

}
